<template>
  <div class="background">
    <a-card style="margin-bottom: 10px">
      <a-row>
        <a-col :span="24">
          <a-form
            layout="inline"
            :form="form"
            @submit="handleSubmit"
          >
            <a-form-item label="模板标题">
              <a-input
                allow-clear
                placeholder="请输入模板标题"
                v-model="title"
              ></a-input>
            </a-form-item>
            <a-form-item label="模板ID">
              <a-input
                allow-clear
                style="width:450px"
                placeholder="请输入模板ID"
                v-model="templateId"
              ></a-input>
            </a-form-item>
            <a-form-item>
              <a-button
                type="primary"
                html-type="submit"
              >搜索</a-button>
            </a-form-item>
            <a-form-item>
              <a-button
                ghost
                type="primary"
                @click="toWx"
              >同步微信模板</a-button>
            </a-form-item>
          </a-form>

        </a-col>
      </a-row>
    </a-card>
    <a-card>
      <a-menu
        v-model="current"
        mode="horizontal"
        @click="handleClick"
      >
        <a-menu-item key="1">我的模板 </a-menu-item>
        <!-- <a-menu-item
            key="2"
          >模板库 </a-menu-item> -->
      </a-menu>
      <div id="change">
        <a-table
          bordered
          :loading="loading"
          :pagination="pagination"
          :columns="columns"
          :data-source="dataSource"
          @change="handleTableChange"
          :scroll="{ x: true }"
        >
          <template
            slot="action"
            slot-scope="txt, record"
          >
            <a
              class="mr-10"
              @click="toDetail(record)"
            >
              <a-icon type="unordered-list" />详情
            </a>
            <!-- <a
            class="mr-10"
            @click="deleteRecord(record)"
          >
            <a-icon type="delete" />删除
          </a> -->
          </template>
        </a-table>
      </div>

    </a-card>
    <detail
      v-if="showDetail"
      :detail="detail"
      @closeDetail="closeDetail"
    ></detail>
  </div>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
import detail from './detail.vue'
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'id'
  },
  {
    title: '模板ID',
    width: 550,
    dataIndex: 'templateId'
  },
  {
    title: '模板标题',
    dataIndex: 'title'
  },
  {
    title: '一级行业',
    dataIndex: 'primaryIndustry'
  },
  {
    title: '二级行业',
    dataIndex: 'deputyIndustry'
  },
  {
    title: '操作',
    width: 100,
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  components: {
    detail
  },
  props: {
    appid: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      columns,
      current: ['1'],
      urlKey: 'basemgt-wx',
      dataSource: [],
      loading: false,
      template: '',
      detail: {},
      menuList: [],
      templateId: '',
      title: '',
      showAdd: false,
      showDetail: false,
      form: this.$form.createForm(this),
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: total => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        }
      }
    }
  },
  mounted() {
    this.loadData()
  },
  watch: {
    appid() {
      this.loadData()
    }
  },
  methods: {
    handleTableChange(pagination) {
      this.pagination = pagination
      this.loadData()
    },

    close() {
      this.showAdd = false
      this.loadData()
    },
    add(record) {
      this.showAdd = true
      this.template = record
    },
    changeMenu() {
      this.loadData()
    },
    toDetail(record) {
      this.detail = record
      this.showDetail = true
    },
    closeDetail() {
      this.showDetail = false
    },
    handleSubmit(e) {
      e.preventDefault()
      this.loadData(1)
    },
    loadData(key) {
      if (key) {
        this.pagination.current = 1
      }
      this.loading = true
      const params = {
        appid: this.appid,
        pageSize: this.pagination.pageSize,
        pageNum: this.pagination.current
      }
      if (this.title) {
        params.title = this.title
      }
      if (this.templateId) {
        params.templateId = this.templateId
      }
      try {
        postAction(`${this.urlKey}/WxStandardTemplate/findWxStandardTemplateListByCondition`, params).then(data => {
          if (data.code == 200) {
            this.dataSource = data.data.records
            this.pagination.total = data.data.total
          } else {
            this.$message.warning(data.msg || data.message)
          }
          this.loading = false
        })
      } catch (error) {
        this.$message.warning('获取列表失败！')
      } finally {
        this.loading = false
      }
    },
    handleClick(e) {
      this.type = e.key
      this.pageNum = 1
      this.loadData(1)
    },
    toWx() {
      this.$confirm({
        content: '确定要同步微信吗？',
        onOk: () => {
          const params = {
            appid: this.appid,
          }
          getAction(`${this.urlKey}/WxStandardTemplate/getWxStandardTemplateByAppid`, params).then(data => {
            if (data.code == 200) {
              this.$message.success('同步成功')
              this.loadData()
            } else {
              this.$message.warning(data.msg)
            }
          })
        },
        onCancel() {}
      })
    },
    deleteRecord(item) {
      this.$confirm({
        content: '确定要删除' + item.title + '吗？',
        onOk: () => {
          const params = {
            appid: this.appid,
            id: item.id,
            templateId: item.templateId
          }
          getAction(`${this.urlKey}/WxStandardTemplate/deleteWxStandardTemplateByConditionId`, params).then(data => {
            if (data.code == 200) {
              this.$message.success('删除成功')
              this.loadData()
            } else {
              this.$message.warning(data.msg || data.message)
            }
          })
        },
        onCancel() {}
      })
    }
  }
}
</script>
<style>
#change .ant-table {
  font-size: 15px;
}
</style>

